<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import $api from '@/api/api'
import { Search } from '@element-plus/icons-vue'

// 搜索表单数据
const searchForm = reactive({
  page: 1,
  pageSize: 10,
  sno: '',
  barCode: '',
  borDate: '',
  retStatus: '',
  bname: ''
})

// 表格数据
const tableData = ref([])
const total = ref(0)
const loading = ref(false)

// 获取借阅记录
const fetchRecords = async () => {
  loading.value = true
  try {
    const res = await $api.getStudentBorrowRecord(searchForm)
    if (res.code === 1) {
      tableData.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error(res.msg || '获取借阅记录失败')
    }
  } catch (error) {
    ElMessage.error('获取借阅记录失败')
  } finally {
    loading.value = false
  }
}

// 处理分页变化
const handlePageChange = (page) => {
  searchForm.page = page
  fetchRecords()
}

// 处理每页条数变化
const handleSizeChange = (size) => {
  searchForm.pageSize = size
  searchForm.page = 1
  fetchRecords()
}

// 处理搜索
const handleSearch = () => {
  searchForm.page = 1
  fetchRecords()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    page: 1,
    pageSize: 10,
    sno: '',
    barCode: '',
    borDate: '',
    retStatus: '',
    bname: ''
  })
  handleSearch()
}

onMounted(() => {
  fetchRecords()
})
</script>

<template>
  <div class="record-manage-page">
    <!-- 搜索区域 -->
    <el-card class="search-card" shadow="hover">
      <div class="search-header">
        <h2>借阅记录管理</h2>
      </div>
      <el-form :model="searchForm" inline class="search-form">
        <el-form-item label="学号">
          <el-input v-model="searchForm.sno" placeholder="请输入学号" clearable />
        </el-form-item>
        <el-form-item label="条形码">
          <el-input v-model="searchForm.barCode" placeholder="请输入图书条形码" clearable />
        </el-form-item>
        <el-form-item label="书名">
          <el-input v-model="searchForm.bname" placeholder="请输入书名" clearable />
        </el-form-item>
        <el-form-item label="借阅日期">
          <el-date-picker v-model="searchForm.borDate" type="date" placeholder="选择借阅日期" style="width: 100%"
            value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="归还状态">
          <el-select v-model="searchForm.retStatus" placeholder="请选择状态" clearable>
            <el-option label="按时还" value="1" />
            <el-option label="超时还" value="2" />
            <el-option label="未归还" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" :icon="Search">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 借阅记录列表 -->
    <el-card class="list-card" shadow="hover">
      <el-table :data="tableData" v-loading="loading" border stripe style="width: 100%">
        <el-table-column prop="sno" label="学号" min-width="120" show-overflow-tooltip />
        <el-table-column prop="bname" label="书名" min-width="150" show-overflow-tooltip />
        <el-table-column prop="isbn" label="ISBN" min-width="180" show-overflow-tooltip />
        <el-table-column prop="barCode" label="条形码" min-width="150" show-overflow-tooltip />
        <el-table-column prop="borDate" label="借阅日期" min-width="120" show-overflow-tooltip />
        <el-table-column prop="retDate" label="应还日期" min-width="120" show-overflow-tooltip />
        <el-table-column prop="realRetDate" label="实际归还日期" min-width="120" show-overflow-tooltip />
        <el-table-column label="归还状态" width="120">
          <template #default="{ row }">
            <el-tag :type="{
              '按时还': 'success',
              '超时还': 'danger',
              '未归还': 'warning'
            }[row.retStatus] || ''">
              {{ row.retStatus || '未知状态' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="罚款状态" min-width="180">
          <template #default="{ row }">
            <div class="fine-info">
              <el-tag :type="row.fineStatus === '1' ? 'success' : row.fineStatus === '0' ? 'danger' : ''"
                v-if="row.fineMoney > 0">
                {{ row.fineStatus === '1' ? '已缴费' : '未缴费' }}
              </el-tag>
              <span v-if="row.fineMoney > 0" class="fine-money">
                ¥{{ row.fineMoney }}
              </span>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination v-model:current-page="searchForm.page" v-model:page-size="searchForm.pageSize"
          :page-sizes="[10, 20, 50, 100]" :total="total" @size-change="handleSizeChange"
          @current-change="handlePageChange" layout="total, sizes, prev, pager, next, jumper" background />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.record-manage-page {
  padding: 20px;
  min-height: 100vh;
  background: linear-gradient(135deg, #f0f7ff 0%, #e4ecfb 50%, #f5f9ff 100%);
  animation: gradientAnimation 15s ease infinite;
  background-size: 400% 400%;
}

@keyframes gradientAnimation {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.search-card {
  margin-bottom: 20px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(70, 130, 180, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.18);
  overflow: hidden;
}

.search-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 36px rgba(70, 130, 180, 0.15);
}

.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 16px 0 0;
  position: relative;
}

.search-header::after {
  content: '';
  position: absolute;
  bottom: -12px;
  left: 0;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg,
  rgba(70, 130, 180, 0) 0%,
  rgba(70, 130, 180, 0.2) 50%,
  rgba(70, 130, 180, 0) 100%
  );
}

.search-header h2 {
  margin: 0;
  font-size: 1.8em;
  color: #2c3e50;
  font-weight: 600;
  background: linear-gradient(90deg, #4682B4, #87CEEB);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 20px 0 10px;
}

:deep(.el-form-item) {
  margin-bottom: 16px;
  transition: transform 0.3s ease;
}

:deep(.el-form-item:hover) {
  transform: translateX(4px);
}

:deep(.el-input__wrapper),
:deep(.el-select .el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(70, 130, 180, 0.08);
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover),
:deep(.el-select .el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(70, 130, 180, 0.12);
}

:deep(.el-button) {
  border-radius: 8px;
  padding: 12px 24px;
  transition: all 0.3s ease;
  font-weight: 500;
}

:deep(.el-button--primary) {
  background: linear-gradient(90deg, #4682B4, #87CEEB);
  border: none;
  box-shadow: 0 4px 12px rgba(70, 130, 180, 0.2);
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(70, 130, 180, 0.3);
  background: linear-gradient(90deg, #5693C5, #98DFF9);
}

:deep(.el-button--default) {
  border: 1px solid #dcdfe6;
  background: linear-gradient(to bottom, #ffffff, #f8f9fb);
}

:deep(.el-button--default:hover) {
  transform: translateY(-2px);
  background: linear-gradient(to bottom, #ffffff, #f0f3f9);
  border-color: #c6e2ff;
}

.list-card {
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(70, 130, 180, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.18);
  overflow: hidden;
}

:deep(.el-table) {
  background: transparent !important;
  border-radius: 8px;
}

:deep(.el-table th) {
  background: rgba(70, 130, 180, 0.05) !important;
  font-weight: 600;
  color: #2c3e50;
}

:deep(.el-table tr) {
  background: transparent !important;
  transition: all 0.3s ease;
}

:deep(.el-table tr:hover td) {
  background: rgba(70, 130, 180, 0.05) !important;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  background: rgba(70, 130, 180, 0.02);
}

.pagination-container {
  margin-top: 24px;
  display: flex;
  justify-content: center;
  padding: 16px 0;
}

:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background: linear-gradient(90deg, #4682B4, #87CEEB);
  box-shadow: 0 2px 8px rgba(70, 130, 180, 0.2);
}

.fine-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

:deep(.el-tag) {
  border-radius: 6px;
  padding: 6px 12px;
  font-weight: 500;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

:deep(.el-tag:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.08);
}

.fine-money {
  color: #f56c6c;
  font-weight: 600;
  background: rgba(245, 108, 108, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.fine-money:hover {
  background: rgba(245, 108, 108, 0.15);
  transform: scale(1.05);
}

/* 响应式布局优化 */
@media (max-width: 768px) {
  .search-form {
    flex-direction: column;
    gap: 8px;
  }

  .search-form :deep(.el-form-item) {
    margin-right: 0;
    width: 100%;
  }

  .search-header h2 {
    font-size: 1.5em;
  }

  :deep(.el-button) {
    width: 100%;
    margin-bottom: 8px;
  }

  .list-card {
    margin-top: 16px;
  }
}

/* 表格响应式优化 */
:deep(.el-table .cell) {
  white-space: normal;
  line-height: 1.5;
  padding: 8px;
}

:deep(.el-table .el-table__cell) {
  padding: 12px 8px;
}

/* 添加加载动画优化 */
:deep(.el-loading-spinner) {
  .circular {
    animation: loading-rotate 2s linear infinite;
  }
  .path {
    stroke: #4682B4;
    stroke-width: 3;
  }
}
</style>
